function matBuild(matType, topToneHeight, topToneWidth, topToneOffset, mainHeight, mainWidth, bottomToneHeight, bottomToneWidth, bottomToneOffset) {
  var totalWidth = 0;
  var totalHeight = 0;

  // 主逻辑
  updateTotalHeightAndTotalWidth();
  createNewDocument();

  var activeDocument = app.activeDocument;
  createLayers();
  createMatShape();
  setRulerOrigin(0, totalHeight);
  backupShapes();
  // 主逻辑结束

  // 测试结果
  // X加500，Y不变
  // setRulerOrigin(MMTopoint(500) * -1, MMTopoint(500));
  // X减500，Y不变
  // setRulerOrigin(MMTopoint(500), MMTopoint(500));
  // X不变，Y加500
  // setRulerOrigin(0, MMTopoint(500) * 2);
  // X不变，Y减1500
  // setRulerOrigin(0, MMTopoint(500) * -2);

  function setRulerOrigin(x, y) {
    activeDocument.rulerOrigin = [x, y];
  }

  function createNewDocument() {
    try {
      // 指定预设文档
      var docProfileName = "CMYK-2025";
      var docPreset = new DocumentPreset();
      docPreset.title = "电梯垫-1";
      docPreset.units = RulerUnits.Millimeters;

      docPreset.width = totalWidth;
      docPreset.height = totalHeight;

      app.documents.addDocument(docProfileName, docPreset);
    } catch (error) {
      var anotherDocPreset = new DocumentPreset();
      anotherDocPreset.title = "电梯垫-1";
      anotherDocPreset.units = RulerUnits.Millimeters;

      anotherDocPreset.width = totalWidth;
      anotherDocPreset.height = totalHeight;

      app.documents.addDocument(DocumentColorSpace.CMYK, anotherDocPreset);
    }
  }

  function getTotalHeight() {
    if (matType == 1) {
      return MMTopoint(mainHeight + topToneHeight);
    } else if (matType == 2) {
      return MMTopoint(mainHeight + bottomToneHeight);
    } else if (matType == 3) {
      return MMTopoint(mainHeight + topToneHeight + bottomToneHeight);
    } else if (matType == 4) {
      return MMTopoint(mainHeight);
    } else {
      return MMTopoint(mainHeight);
    }
  }

  function getTotalWidth() {
    return MMTopoint(mainWidth);
  }

  function updateTotalHeightAndTotalWidth() {
    totalHeight = getTotalHeight();
    totalWidth = getTotalWidth();
  }

  // 转换单位
  // 实际大小
  function pointToMM(numIn) {
    return (numIn / 72) * 25.4;
  }
  function MMTopoint(numIn) {
    return (numIn / 25.4) * 72;
  }
  // 转换成米，生产大小
  function pointToM(numIn) {
    return (((numIn / 72) * 25.4) / 1000) * drawingScale;
  }
  // 转换成点
  function MTopoint(numIn) {
    return (((numIn * 1000) / 25.4) * 72) / drawingScale;
  }

  function formatNum(numIn, accuracyIn) {
    var tempNum = numIn.toFixed(accuracyIn + 2).slice(0, -1);
    var tempNumString = "" + tempNum;
    var lastNum = parseInt(tempNumString.charAt(tempNumString.length - 1));
    if (lastNum != 0) {
      var zeroString = "";
      var tempString = "";
      for (k = 1; k < accuracyIn; k++) {
        zeroString += "0";
      }
      tempString = "0." + zeroString + "1";
      // 此时area为字符串
      numIn = numIn.toFixed(accuracyIn + 1).slice(0, -1);
      numIn = parseFloat(numIn) + parseFloat(tempString);
    } else {
      numIn = numIn.toFixed(accuracyIn + 1).slice(0, -1);
    }
    return numIn;
  }
  
  function trimEndZero(numIn) {
    var originString = "" + numIn;
    numIn = parseFloat(originString);
    return numIn;
  }

  function createLayers() {
    //更改默认图层名称
    var ifExist = false;
    for (i = 0; i < activeDocument.layers.length; i++) {
      if (activeDocument.layers[i].name == "图形") {
        ifExist = true;
      }
    }
    if (!ifExist) {
      currentLayer = activeDocument.layers[0];
      currentLayer.name = "图形";
    }

    //建立bk图层
    var ifExist = false;
    for (i = 0; i < activeDocument.layers.length; i++) {
      if (activeDocument.layers[i].name == "备份") {
        ifExist = true;
      }
    }
    if (!ifExist) {
      newLayer = activeDocument.layers.add();
      newLayer.name = "备份";
      newLayer.zOrder(ZOrderMethod.SENDTOBACK);
    }
    //建立PSD图层
    var ifExist = false;
    for (i = 0; i < activeDocument.layers.length; i++) {
      if (activeDocument.layers[i].name == "PSD") {
        ifExist = true;
      }
    }
    if (!ifExist) {
      newLayer = activeDocument.layers.add();
      newLayer.name = "PSD";
      newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
    }
    //建立tag图层
    var ifExist = false;
    for (i = 0; i < activeDocument.layers.length; i++) {
      if (activeDocument.layers[i].name == "标注") {
        ifExist = true;
      }
    }
    if (!ifExist) {
      newLayer = activeDocument.layers.add();
      newLayer.name = "标注";
      newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
    }
    //建立cut图层
    var ifExist = false;
    for (i = 0; i < activeDocument.layers.length; i++) {
      if (activeDocument.layers[i].name == "切割") {
        ifExist = true;
      }
    }
    if (!ifExist) {
      newLayer = activeDocument.layers.add();
      newLayer.name = "切割";
      newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
    }
  }

  function getRandomCMYKColorValue() {
    return Math.floor(Math.random() * 101);
}

  function createCube(x, y, width, height) {
    var cube = currentLayer.pathItems.rectangle(x, y, width, height);

    try {
      var newColor = new CMYKColor();
      newColor.cyan = getRandomCMYKColorValue();
      newColor.magenta = getRandomCMYKColorValue();
      newColor.yellow = getRandomCMYKColorValue();
      newColor.black = 0;
      cube.fillColor = newColor;
      cube.stroked = false;
    } catch (error) {
      alert("颜色模式不是CMYK");
    }
  }

  function createMatShape() {
    // 定位背景图层
    for (i = 0; i < activeDocument.layers.length; i++) {
      if (activeDocument.layers[i].name == "图形") {
        currentLayer = activeDocument.layers[i];
      }
    }

    // 创建基本图形
    if (matType == 1) {
      // 主体
      createCube(MMTopoint(mainHeight), 0, MMTopoint(mainWidth), MMTopoint(mainHeight));
      // 上舌头
      createCube(totalHeight, MMTopoint(topToneOffset), MMTopoint(topToneWidth), MMTopoint(topToneHeight));
    } else if (matType == 2) {
      // 主体
      createCube(MMTopoint(mainHeight + bottomToneHeight), 0, MMTopoint(mainWidth), MMTopoint(mainHeight));
      // 下舌头
      createCube(MMTopoint(bottomToneHeight), MMTopoint(bottomToneOffset), MMTopoint(bottomToneWidth), MMTopoint(bottomToneHeight));
    } else if (matType == 3) {
      // 主体
      createCube(MMTopoint(mainHeight + bottomToneHeight), 0, MMTopoint(mainWidth), MMTopoint(mainHeight));
      // 上舌头
      createCube(totalHeight, MMTopoint(topToneOffset), MMTopoint(topToneWidth), MMTopoint(topToneHeight));
      // 下舌头
      createCube(MMTopoint(bottomToneHeight), MMTopoint(bottomToneOffset), MMTopoint(bottomToneWidth), MMTopoint(bottomToneHeight));
    } else if (matType == 4) {
      createCube(totalHeight, 0, totalWidth, totalHeight);
    }
  }

  function backupShapes() {
    var doc = app.activeDocument;
    var backupLayer = doc.layers.getByName("备份");

    // 遍历所有路径项
    for (var z = doc.pathItems.length - 1; z >= 0; z--) {
        var pathItem = doc.pathItems[z];

        // 检查路径
        if (pathItem.closed) {
            // 获取矩形的边界框
            var bounds = pathItem.geometricBounds;

            // 确定矩形的宽度和高度
            var width = bounds[2] - bounds[0];
            var height = bounds[1] - bounds[3];

            // 创建一个新的矩形并复制属性
            var newRectangle = backupLayer.pathItems.rectangle(bounds[1], bounds[0], width, height);
            newRectangle.fillColor = pathItem.fillColor;
            newRectangle.strokeColor = pathItem.strokeColor;
            newRectangle.strokeWidth = pathItem.strokeWidth;
        }
    }

    backupLayer.visible = false;
  }
}
